using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace ClientLoginPoC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region LOGIN & PASSWORD
            string login = "";
            string pwd = "";
            #endregion

            // Build and convert the POST body
            string postText = "";
            postText += "accountType=GOOGLE";
            postText += "&";
            postText += "Email=" + login;
            postText += "&";
            postText += "Passwd=" + pwd;
            postText += "&";
            postText += "service=mail";
            postText += "&";
            postText += "source=googletalk";
            byte[] postData = Encoding.ASCII.GetBytes(postText);

            // Create the request
            HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin");
            authRequest.Method = "POST";
            authRequest.ContentType = "application/x-www-form-urlencoded";
            authRequest.ContentLength = postData.Length;
            Stream requestStream = authRequest.GetRequestStream();
            requestStream.Write(postData, 0, postData.Length);
            requestStream.Close();
            
            // Get the response in the form of a stream
            HttpWebResponse response = (HttpWebResponse)authRequest.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader responseReader = new StreamReader(responseStream,Encoding.ASCII);
                string responseText = responseReader.ReadToEnd();
                response.Close();
                string[] values = responseText.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                string auth = "";
                string sid = "";
                string lsid = "";
                foreach (string value in values)
                {
                    if (value.StartsWith("Auth="))
                        auth = value.Substring(5);
                    else if (value.StartsWith("SID="))
                        sid = value.Substring(4);
                    else if (value.StartsWith("LSID="))
                        lsid = value.Substring(5);
                }
                //query = "SID=#{@pre_tokens['SID']}&LSID=#{@pre_tokens['LSID']}&service=gaia&Session=true"


                postText = "";
                postText += "SID="+sid;
                postText += "&";
                postText += "LSID=" + lsid;
                postText += "&";
                postText += "service=gaia&Session=true";
                postData = Encoding.ASCII.GetBytes(postText);

                // Create the request
                HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/IssueAuthToken");
                tokenRequest.Method = "POST";
                tokenRequest.ContentType = "application/x-www-form-urlencoded";
                tokenRequest.ContentLength = postData.Length;
                requestStream = tokenRequest.GetRequestStream();
                requestStream.Write(postData, 0, postData.Length);
                requestStream.Close();

                string tokenText = "";
                response = (HttpWebResponse)tokenRequest.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    responseStream = response.GetResponseStream();
                    responseReader = new StreamReader(responseStream, Encoding.ASCII);
                    tokenText = responseReader.ReadToEnd();
                    response.Close();
                }
                else
                    response.Close();


                string url = "https://www.google.com/accounts/TokenAuth?auth=";
                url += tokenText.TrimEnd();
                url += "&service=mail&source=googletalk";
                url += "&continue=https://mail.google.com/mail";
            }
            else
                response.Close();
        }
    }
}